#include "NameManager.h"
#include "RandomSystem.h"

#ifdef DARIO
#include "Utilities.h"
#endif

#include <cassert>

NameManager::NameManager() : m_loaded(false)
{
	bool isLoaded = LoadFromFile("names.txt","surnames.txt", "teams.txt");
	assert(isLoaded);
}

bool NameManager::LoadFromFile(const STL::string& i_Names , const STL::string& i_Surnames, const STL::string& i_teams)
{
	STL::string s;

	STD::ifstream infile(i_Names.c_str());
	if (infile.fail()) 
		return false;
	    
	while (getline(infile, s))
		m_names.push_back(s);

	infile.close();

#ifdef DARIO
	STD::ifstream infileX((STL::string("fun_") + i_Names).c_str());
	if (!infileX.fail()) 
	{
		unsigned int oldNamesSize = m_names.size();
		strContainer funNames;
		while (getline(infileX, s))
		{
			Utilities::RemoveAll_unordered(m_names, s);
    		funNames.push_back(s);
		}
		if(oldNamesSize != m_names.size())
		{
			STD::ofstream outFile(i_Names.c_str());
			Utilities::Sort(m_names);
			for(size_t i=0; i<m_names.size(); ++i)
				outFile << m_names[i] << STD::endl;
		}
		for(size_t i=0; i< funNames.size(); ++i)
			m_names.push_back(funNames[i]);
	}
#endif

	STD::ifstream infile2(i_Surnames.c_str());
	if (infile2.fail()) 
		return false;

	while (getline(infile2, s))
		m_surnames.push_back(s);

	infile2.close();

#ifdef DARIO
	STD::ifstream infile2X((STL::string("fun_") + i_Surnames).c_str());
	if (!infile2X.fail()) 
	{
		unsigned int oldSurnamesSize = m_surnames.size();
		strContainer funSurnames;
		while (getline(infile2X, s))
		{
			Utilities::RemoveAll_unordered(m_surnames, s);
    		funSurnames.push_back(s);
		}
		if(oldSurnamesSize != m_surnames.size())
		{
			STD::ofstream outFile(i_Surnames.c_str());
			Utilities::Sort(m_surnames);
			for(size_t i=0; i<m_surnames.size(); ++i)
				outFile << m_surnames[i] << STD::endl;
		}
		for(size_t i=0; i< funSurnames.size(); ++i)
			m_surnames.push_back(funSurnames[i]);
	}
#endif

	STD::ifstream infile3(i_teams.c_str());
	if (infile3.fail()) 
		return false;

	while (getline(infile3, s))
	{
    	m_teams.push_back(s);
	}

	m_loaded = true;

	return true;
}

STL::string NameManager::GetAPeopleName() const
{
	if (!IsLoaded()) 
		return "";

	return m_surnames[Rand()%(m_surnames.size())] + " "+ m_names[Rand()%(m_names.size())];
}

STL::string NameManager::GetATeamName() const
{
	if (!IsLoaded()) 
		return "";
	
	int selected = Rand()%(m_teams.size());
	while (
		STL::find( m_usedNames.begin(), m_usedNames.end(), selected ) != m_usedNames.end()
		)
	{
		selected = Rand()%(m_teams.size());
	}
	
	m_usedNames.push_back (selected);
	return m_teams[selected];
}

void NameManager::SetTeamNameUsed(const STL::string& i_name) const
{
	
	strContainer::const_iterator it = STL::find( m_teams.begin(), m_teams.end(), i_name);

	if(it == m_teams.end())
		return;

	size_t index = it - m_teams.begin();
	
	m_usedNames.push_back(index);
}

void NameManager::Clear()
{
	m_usedNames.clear();
}
